package Builder.entity;

import java.util.Objects;


public class RabbitMQClient {

    private RabbitMQClient(Builder builder) {

    }

    public void sendMessage(String msg) {
        System.out.println("正在发送消息：" + msg);
    }

    public static class Builder {
        private String host = "127.0.0.1";
        private int port = 7359;
        private int mode;
        private String exchange;
        private String queue;
        private boolean durable = true;
        private int connectionTimeout = 20;

        public Builder setHost(String host) {
            this.host = host;
            return this;
        }

        public Builder setPort(int port) {
            this.port = port;
            return this;
        }

        public Builder setMode(int mode) {
            this.mode = mode;
            return this;
        }

        public Builder setExchange(String exchange) {
            this.exchange = exchange;
            return this;
        }

        public Builder setQueue(String queue) {
            this.queue = queue;
            return this;
        }

        public Builder setDurable(boolean durable) {
            this.durable = durable;
            return this;
        }

        public Builder setConnectionTimeout(int connectionTimeout) {
            this.connectionTimeout = connectionTimeout;
            return this;
        }

        public RabbitMQClient build() {
            // 实现参数校验逻辑
            if (mode == 1) {
                if (Objects.isNull(exchange)) {
                    throw new RuntimeException("工作队列模式无需设计交换机");
                }
                if (Objects.isNull(queue)) {
                    throw new RuntimeException("工作队列模式无需设计队列名称");
                }
                if (durable == false) {
                    throw new RuntimeException("工作队列模式队列必须持久化");
                }
            } else if (mode == 2) {
                if (Objects.isNull(exchange)) {
                    throw new RuntimeException("发布订阅模式必须设计交换机");
                }
                if (Objects.isNull(queue)) {
                    throw new RuntimeException("发布订阅模式必须设计队列名称");
                }
            }

            return new RabbitMQClient(this);
        }
    }

}
